﻿using System;
using System.Security.Cryptography;
using System.Linq;

namespace Grundy.CryptographicServices
{
    public class RsaProvider : CryptographicServicesProvider
    {
        private readonly RSACryptoServiceProvider _selfProvider;
        private const int KEY_SIZE = 1024;

        public RsaProvider(string selfKey)
            : base(selfKey)
        {
            var selfProvider = new RSACryptoServiceProvider(KEY_SIZE);
            selfProvider.PersistKeyInCsp = false;
            selfProvider.FromXmlString(selfKey);
            _selfProvider = selfProvider;
        }

        public override string Algorithm
        {
            get { return _selfProvider.SignatureAlgorithm; }
        }

        public override string Provider
        {
            get { return "RsaProvider"; }
        }

        protected override byte[] SignData(byte[] data)
        {
            return _selfProvider.SignData(data, SHA256Managed.Create());
        }

        protected override bool VerifyData(byte[] data, byte[] signature)
        {
            return _selfProvider.VerifyData(data, SHA256Managed.Create(), signature);
        }

        public override byte[] EncryptData(byte[] data)
        {
            return _selfProvider.Encrypt(data, true);
        }

        public override byte[] DecryptData(byte[] data)
        {
            return _selfProvider.Decrypt(data, true);

        }
    }
}